<?php
/**
 * Copyright (c) Xerox Corporation, Codendi Team, 2001-2009. All rights reserved
 * Copyright Enalean (c) 2015. All rights reserved.
 *
 * Tuleap and Enalean names and logos are registrated trademarks owned by
 * Enalean SAS. All other trademarks or names are properties of their respective
 * owners.
 *
 * This file is a part of Tuleap.
 *
 * Tuleap is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Tuleap is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Tuleap. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * The base class for static fields in trackers.
 * Static Fields are not real fields, as they don't have a specific value for each artifact.
 * The value can be updated, but is the same for every artifact.
 */
abstract class Tracker_FormElement_StaticField extends Tracker_FormElement {

	/**
     * getLabel - the label of this Tracker_FormElement_Line_Break
     * The tracker label can be internationalized.
     * To do this, fill the name field with the ad-hoc format.
     *
     * @return string label, the name if the name is not internationalized, or the localized text if so
     */
    function getLabel() {
        global $Language;
        if ($this->isLabelMustBeLocalized()) {
            return $Language->getText('plugin_tracker_common_staticfield', $this->label);
        } else {
            return $this->label;
        }
    }

    /**
     * Returns if the static field name must be localized or not.
     * The 'form element static field' name must be localized if the name looks like staticfield_{$field_id}_lbl_key
     *
     * @return true if the static field name must be localized, false otherwise.
     */
    function isLabelMustBeLocalized() {
        $pattern = "/staticfield_(.*)_lbl_key/";
        return preg_match($pattern, $this->label);
    }

    /**
     * getDescriptionText - the text of the description of this Tracker_FormElement_FieldSet
     * The tracker descripiton can be internationalized.
     * To do this, fill the description field with the ad-hoc format.
     *
     * @return string description, the description text if the description is not internationalized, or the localized text if so
     */
    function getDescriptionText() {
        global $Language;
        if ($this->isDescriptionMustBeLocalized()) {
            return $Language->getText('plugin_tracker_common_staticfield', $this->description);
        } else {
            return $this->description;
        }
    }

    /**
     * Returns if the static field description must be localized or not.
     * The static field description must be localized if the name looks like staticfield_{$field_id}_desc_key
     *
     * @return true if the static field description must be localized, false otherwise.
     */
    function isDescriptionMustBeLocalized() {
        $pattern = "/staticfield_(.*)_desc_key/";
        return preg_match($pattern, $this->description);
    }


	// TODO : remove these functions (no need for that kind of "fields"
    public function fetchAddCriteria($used, $prefix = '') {
        return null;
    }

    public function fetchAddColumn($used, $prefix = '') {
        return null;
    }

    public function fetchAddTooltip($used, $prefix = '') {
        return null;
    }

    /**
     * Fetch the element for the update artifact form
     *
     * @param Tracker_Artifact $artifact
     *
     * @return string html
     */
    public function fetchArtifact(Tracker_Artifact $artifact) {
        return $this->fetchReadOnly();
    }

    /**
     *
     * @param Tracker_Artifact $artifact
     * @return string html
     */
    public function fetchArtifactForOverlay(Tracker_Artifact $artifact) {
        return $this->fetchArtifact($artifact);
    }

    public function fetchSubmitForOverlay($submitted_values) {
        return $this->fetchSubmit();
    }

    /**
     * Fetch the element for the artifact in read only mode
     *
     * @param Tracker_Artifact $artifact
     *
     * @return string html
     */
    public function fetchArtifactReadOnly(Tracker_Artifact $artifact) {
        return $this->fetchReadOnly();
    }

    /**
     * Fetch the element for the submit new artifact form
     *
     * @return string html
     */
    public function fetchSubmit() {
        return $this->fetchReadOnly();
    }

    /**
     * Fetch the element for the submit new artifact form
     *
     * @return string html
     */
    public function fetchSubmitMasschange() {
        return $this->fetchReadOnly();
    }
    /**
     * Say if the field is updateable
     *
     * @return bool
     */
    public function isUpdateable() {
        return false;
    }

    /**
     * Say if the field is submitable
     *
     * @return bool
     */
    public function isSubmitable() {
        return false;
    }

    /**
     * Is the form element can be removed from usage?
     * This method is to prevent tracker inconsistency
     *
     * @return string returns null if the field can be unused, a message otherwise
     */
    public function getCannotRemoveMessage() {
        return '';
    }

    public function canBeRemovedFromUsage() {
        return true;
    }

    /**
     * return true if user has Read or Update permission on this field
     *
     * @param PFUser $user The user. if not given or null take the current user
     *
     * @return bool
     */
    public function userCanRead(PFUser $user = null) {
        return true;
    }

    protected abstract function fetchReadOnly();

    /**
     * @see Tracker_FormElement::fetchArtifactCopyMode
     */
    public function fetchArtifactCopyMode(Tracker_Artifact $artifact, $submitted_values = array())
    {
        return $this->fetchArtifactReadOnly($artifact, $submitted_values);
    }

    /**
     * Accessor for visitors
     *
     * @param Tracker_FormElement_Visitor $visitor
     */
    public function accept(Tracker_FormElement_Visitor $visitor) {
        $visitor->visit($this);
    }

    /**
     * Get available values of this field for SOAP usage
     *
     * @return mixed The values or null if there are no specific available values
     */
    public function getSoapAvailableValues() {
        return null;
    }

    /**
     * Get binding data for Soap
     *
     * @return array the binding data
     */
    public function getSoapBindingProperties() {
        return array(
            'bind_type' => null,
            'bind_list' => array()
        );
    }

    public function isCollapsed() {
        return false;
    }

    public function getDefaultValue() {
        return null;
    }

    public function getDefaultRESTValue() {
        return $this->getDefaultValue();
    }
}